package engine.logic.walk
{
	import engine.data.ModelLocator;
	import engine.data.SceneObject;
	import engine.flag.ObjType;
	import engine.newUi.DuplicatesLogic;
	import engine.newUi.DuplicatesUI;
	import engine.scene.player.BasePlayerModel;
	import engine.ui.MainUIManager;
	import engine.ui.UIManage;
	import engine.ui.lite.BaseUI;
	
	import flash.display.Sprite;
	import flash.geom.Point;
	
	import newx.BaseObject;
	
	public class NpcIconManage
	{
		private static var _self		:NpcIconManage;
		private var _NpcItems			:Object;
		private var _NpcOpenUI			:Object;
		private var _hotNum			:int	=	100;
		private var _hotHight			:int	=	100;
		private var _model				:ModelLocator;
		private var _taskItem			:Array;
		private var _TaskItems			:Object;
		
		public function NpcIconManage()
		{
			_NpcItems	=	{};
			_NpcOpenUI	=	{};
			_TaskItems	=	{};
			_model		=	ModelLocator.getInstance();	
		}
		
		public static function getInstance():NpcIconManage
		{
			if(_self == null)
			{
				_self	=	new NpcIconManage();
			}
			return _self;
		}
		public function addNpcItems(val:Sprite,key:String,obj:SceneObject):void
		{
			if(obj != null)
			{
				var item:Object	=	{};
				item["target"]	=	val;
				item["type"]	=	obj.Type;
				item["obj"]		=	obj;
				item["isOpen"]	=	0;
				_NpcItems[key]	=	item;
			}
		}
		public function clearNpcItems():void
		{
			_NpcItems	=	{};
		}
		public function playMoveCheck(x_:int , y_:int):void
		{
			if( MainUIManager.getInstance().autoFoundWay )
			{//自动寻路的时候
				return;
			}
			
			for(var p:String in _NpcItems)
			{
				var checkObj:Object					=	_NpcItems[p] as Object
				var checkNpcItem:BasePlayerModel	=	checkObj["target"];
				//检查门点
				if(checkObj["type"] == ObjType.Door)
				{
					var xPoint : int = Math.abs(checkNpcItem.x - x_);
					var yPoint : int = Math.abs(checkNpcItem.y - y_);
					if( xPoint <= _hotNum && yPoint <= _hotHight)
					{
						if( DuplicatesLogic.getInstance().isOpen )
						{	
							moveDoorOverFun(checkObj["obj"]);
							DuplicatesLogic.getInstance().isOpen = false;
						}
					}
					else if( xPoint > _hotNum + 100 )
					{
						DuplicatesLogic.getInstance().isOpen = true;
						if(DuplicatesUI.getInstance().created)
						{
							DuplicatesUI.getInstance().hide();
						}
					}
					break;
				}
			}
		}
		public function addTalkNpc(val:Point,uiName:BaseUI):void
		{
			var tempScene:SceneObject	=	_model.scene.GetObject(val);
			if(tempScene != null)
			{
				var key:String				=	tempScene.GetPropertiesEx("ScenePos");
				_NpcOpenUI[key]				=	uiName;
			}
		}

		private function removeUI(val:String):void
		{
			for(var p:String	in _NpcOpenUI)
			{
				if(val == p)
				{
					var showUI:BaseUI	=	_NpcOpenUI[p] as BaseUI;
					UIManage.getInstance().requestHideUI(showUI);
					delete _NpcOpenUI[p];
				}
			}
		}
		
		/**
		 * 移到结束后的回调函数
		 * 显示门点
		 */
		private function moveDoorOverFun(obj:BaseObject):void
		{
			if ( null != obj && obj.CheckProperties("EnterFB")) 
			{
				
				var FBListID:int			=	obj.GetProperties("EnterFB");
				var type:int				=	obj.GetProperties("DoorType");
				if(type == 1)
				{
					DuplicatesLogic.getInstance().showInfo("FB_" + FBListID);
				}
				else if(type == 2)
				{
					DuplicatesLogic.getInstance().showInfo("JY_" + FBListID);
				}
				
			}
		}
	}
}